﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEK02LIST : DocumentBase
    {
        #region-------------Properties-----------------------
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public ITXEXRPT ITXEXRPT; 
        public YCAEK02LISTData m_Data;  
        public string PrnType = "EXCEL";
        BindingListCollectionView cvEK0201
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgEK0201.ItemsSource);
            }
        }
        BindingListCollectionView cvEK0202
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgEK0202.ItemsSource);
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB Set4ITX2
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX2");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }

        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS=======
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS2=======
        CollectionViewSource vwEKPOS2
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS2");
            }
        }
        DataRow dwEKPOS2
        {
            get
            {
                if (vwEKPOS2.View == null)
                    return null;
                if ((DataRowView)vwEKPOS2.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS2.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS3=======
        CollectionViewSource vwEKPOS3
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS3");
            }
        }
        DataRow dwEKPOS3
        {
            get
            {
                if (vwEKPOS3.View == null)
                    return null;
                if ((DataRowView)vwEKPOS3.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS3.View.CurrentItem).Row;
            }
        }
        #endregion

        #endregion
        string Year
        {
            get
            {
                if (txtYear.Text.Trim() == "")
                {
                    MessageBox.Show("请设定年限");
                }
                return Helpers.Helper.Left(txtYear.Text.ToString() + "    ", 5);
            }
            set
            {
                txtYear.Text = value;
            }
        }
        string SYear
        {
            get
            {
                if (txtSYear.Text.Trim() == "")
                {
                    //MessageBox.Show("请设定年限");
                }
                return Helpers.Helper.Left(txtSYear.Text.ToString() + "    ", 5);
            }
            set
            {
                txtSYear.Text = value;
            }
        }
        public YCAEK02LIST()
        {
            InitializeComponent();
            this.Name = "采购统计";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            m_Data = new YCAEK02LISTData();
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                SetEnv.Merge(m_Data.LoadSupplier());
              
                DataRow dwData = SetData.FSSTAMM.NewRow();
                dwData["FSMANDANT"] = "00";
                DataSet SResult = INTEXDBC.QueryFSSTAMM(dwData, 7);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    txtYear.Text = row["FSKOLL"].ToString();
                    txtSYear.Text = txtYear.Text;
                }
                txtDate.Text = m_Data.year + m_Data.month;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        public void btnQuery1_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                DataRow dwData = SetData.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["FSKOLL"] = SYear;
                dwData["FSABEZ"] = Helpers.Helper.ParseSectionStr(txtSection.Text);
                dwData["ABPART"] = txtArt.Text.Trim();
                dwData["ABAART"] = txtFArt.Text.Trim();
                Result = m_Data.BuildData(dwData, 1);
                Set4ITX2.Clear();
                Set4ITX2.Merge(Result);
                if (vwEKPOS2.View != null)
                {
                    vwEKPOS2.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                if (txtSection.Text.Trim() == "")
                {
                    MessageBox.Show("请指定季节");
                    return;
                }
                DataRow dwData = SetData.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["FSKOLL"] = Year;
                dwData["ABPART"] = Helpers.Helper.ParseSectionStr(txtSection.Text);
                Result = m_Data.BuildData(dwData);
                Set4ITX.Clear();
                Set4ITX.Merge(Result);
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnFilterEK0201_Click(object sender, RoutedEventArgs e)
        {
            if (Set4ITX.EKPOS.Rows.Count == 0)
            {
                MessageBox.Show("请先执行查询");
                btnQuery_Click(null, null);
                return;
            }
            string filter = "LSLIEFNA ='" + dwLIEFDAT["LSLIEFNA"].ToString() + "'";
            cvEK0201.CustomFilter = filter;
        }
        private void btnFilterEK0201Clear_Click(object sender, RoutedEventArgs e)
        {
            cvEK0201.CustomFilter = null;
        }
        private void btnFilterEK0202_Click(object sender, RoutedEventArgs e)
        {
            if (Set4ITX2.EKPOS.Rows.Count == 0)
            {
                MessageBox.Show("请先执行查询");
                btnQuery1_Click(null, null);
                return;
            }
            string filter = "ABPART LIKE '%" + txtArt.Text.ToString().Trim() + "%' AND ABAART LIKE '%" + txtFArt.Text.ToString().Trim() + "%'";
            cvEK0202.CustomFilter = filter;
        }
        private void btnFilterEK0202Clear_Click(object sender, RoutedEventArgs e)
        {
            cvEK0202.CustomFilter = null;
        }
        public void btnPrintPreview1_Click(object sender, EventArgs e)
        { 
            ITXEXRPT.dsSource = Set4ITX2;
            ITXEXRPT.DocType = "YCAEKAB15";
            ITXEXRPT.strExcelFile = app.company + @"\YCAEK02LIST.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        private void cbSECTION_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            string section = cb.SelectedValue + ";";
            if (txtSection.Text.Contains(section))
                return;
            txtSection.Text += section;
        }

        public void btnPrintPreview_Click(object sender, EventArgs e)
        {
            ITXEXRPT.dsSource = Set4ITX;
            ITXEXRPT.DocType = "YCAEK02LIST";
            ITXEXRPT.strExcelFile = app.company + @"\YCAEK02LIST.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        public void btnQuery3_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = "00";
                dwData["EKPWAKZ"] = " ";
                if (ChkFEKME3.IsChecked == true)
                {
                    Result = INTEXDBC.QueryICC_EKPOS(dwData, 6);
                }
                Set4ITX2.Clear();
                Set4ITX2.Merge(Result);
                if (vwEKPOS3.View != null)
                {
                    vwEKPOS3.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void btnQuery4_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = "00";
                dwData["EKPWAKZ"] = "F";
                if (ChkFEKME4.IsChecked == true)
                {
                    Result = INTEXDBC.QueryICC_EKPOS(dwData, 6);
                }
                Set4ITX2.Clear();
                Set4ITX2.Merge(Result);
                if (vwEKPOS3.View != null)
                {
                    vwEKPOS3.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQueryHistory_Click(object sender, RoutedEventArgs e)
        {
            string mYear = txtDate.Text.ToString().Trim();
            DataRow dwData = SetData.EKVRECH.NewRow();
            dwData["EKVAENDDAT"] = mYear;
            DataSet SResult = INTEXDBC.QueryEKVRECH(dwData, 4);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["SELLER"] = Helpers.Helper.Convert8859P1ToGB2312(row["SELLER"].ToString().Trim());
                row["SELLERNAME"] = Helpers.Helper.Convert8859P1ToGB2312(row["SELLERNAME"].ToString().Trim());
                row["EXFTY"] = Helpers.Helper.Convert8859P1ToGB2312(row["EXFTY"].ToString().Trim());
                row["FACNAME"] = Helpers.Helper.Convert8859P1ToGB2312(row["FACNAME"].ToString().Trim());
                row["FSABEZ"] = Helpers.Helper.Convert8859P1ToGB2312(row["FSABEZ"].ToString().Trim());
                row["EXART"] = Helpers.Helper.Convert8859P1ToGB2312(row["EXART"].ToString().Trim());
                row["EXCOLOR"] = Helpers.Helper.Convert8859P1ToGB2312(row["EXCOLOR"].ToString().Trim());
                row["CDES"] = Helpers.Helper.Convert8859P1ToGB2312(row["CDES"].ToString().Trim());
                if (row["MTYPE"].ToString() == "T")
                {
                    row["MATTYP"] = "辅料";
                    continue;
                }
                switch (row["FSSZUSCHN"].ToString().Trim())
                {
                    case "J":
                        row["MATTYP"] = "主面料";
                        break;
                    case "":
                        row["MATTYP"] = "里布";
                        break;
                    case "C":
                        row["MATTYP"] = "衬布";
                        break;
                    case "N":
                        row["MATTYP"] = "配布";
                        break;
                }
            }
            SetData.Clear();
            SetData.Merge(SResult);
        }

        private void btnPrintHistory_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.DocType = app.company + "EKPAID";
            ITXEXRPT.strExcelFile = app.company + @"\YCAEK.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

    }
}
